home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / sw / stars.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.1 KB  |  138 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "sw.h"
  18. #include "extern.h"
  19. #include "stars.h"
  20. #include "main.h"
  21. #include <Inventor/actions/SoGLRenderAction.h>
  22. #include <Inventor/nodes/SoSeparator.h>
  23. #include <Inventor/nodes/SoTranslation.h>
  24. #include <Inventor/nodes/SoCoordinate3.h>
  25. #include <Inventor/nodes/SoPointSet.h>
  26. #include <Inventor/nodes/SoLightModel.h>
  27. #include <Inventor/nodes/SoMaterialBinding.h>
  28. #include <Inventor/nodes/SoMaterial.h>
  29. #include <Inventor/nodes/SoComplexity.h>
  30. #include <Inventor/nodes/SoSphere.h>
  31. #include <Inventor/nodes/SoCallback.h>
  32. #include <gl/gl.h>
  33. #include <gl/glws.h>
  34.  
  35. #define    STARRADIUS    (100.0)
  36. #define    SUNRADIUS    (STARRADIUS / 20.0)
  37. #define    SUNSIDES    12
  38.  
  39. static float        starColors[][3] = {
  40.                 {1.0, 1.0, 1.0},
  41.                 {1.0, 1.0, 0.5},
  42.                 {1.0, 0.5, 0.5},
  43.                 {1.0, 1.0, 1.0},
  44.                 {1.0, 1.0, 0.5},
  45.                 {0.5, 0.5, 1.0} };
  46. #define    NUMSTARCOLORS    (sizeof(starColors)/sizeof(float[3]))
  47. static float        sunColor[3] = { 1.0, 1.0, 0.5 };
  48. static float        (*starPositions)[3],
  49.             (*sunVertices)[3];
  50. static int        numStars,
  51.             numStarsPerColor;
  52. static int        starsFrameNumber = -1;
  53.  
  54. // The stars must be drawn before the camera is translated so that they
  55. // stay a constant distance from the ship.  Also they must be drawn
  56. // without modifying the depth buffer so they never appear above objects
  57. // in local space.  This also means they must be drawn before local
  58. // objects.
  59.  
  60. static void        drawStars()
  61. {
  62.   if (!isPaused() && frameNumber == starsFrameNumber)    // avoid extra redraws
  63.     return;
  64.   starsFrameNumber = frameNumber;
  65.   frameDrawn = TRUE;
  66.  
  67.   GLXwinset(display, view->getNormalWindow());
  68.  
  69.   zwritemask(0x0);                // don't touch the z buffer
  70.  
  71.   // draw stars
  72.   for (int i = 0, k = 0; i < NUMSTARCOLORS; i++) {
  73.     c3f(starColors[i]);                // new color
  74.     bgnpoint();
  75.       for (int j = 0; j < numStarsPerColor; k++, j++)
  76.     v3f(starPositions[k]);
  77.     endpoint();
  78.   }
  79.   if (k < numStars) {                // finish off remaining stars
  80.     bgnpoint();
  81.       for (; k < numStars; k++)
  82.     v3f(starPositions[k]);
  83.     endpoint();
  84.   }
  85.  
  86.   // draw sun
  87.   c3f(sunColor);
  88.   for (i = 1; i < SUNSIDES-1; i++) {
  89.     bgnpolygon();
  90.       v3f(sunVertices[0]);
  91.       v3f(sunVertices[i+1]);
  92.       v3f(sunVertices[i]);
  93.     endpolygon();
  94.   }
  95.  
  96.   zwritemask(0xffffffff);            // allow z buffer writes
  97. }
  98.  
  99. static void        starsAction(void*, SoAction* action)
  100. {
  101.   if (action->getTypeId() == SoGLRenderAction::getClassTypeId())
  102.     drawStars();
  103. }
  104.  
  105. SoNode*            makeStars(int num)
  106. {
  107.   SoSeparator* starRoot = new SoSeparator;    // all stars under here
  108.  
  109.   sunVertices = (float(*)[3])new float[SUNSIDES*3];
  110.   for (int i = 0; i < SUNSIDES; i++) {
  111.     sunVertices[i][0] = STARRADIUS;
  112.     sunVertices[i][1] = SUNRADIUS * cos(2.0 * M_PI * float(i) / SUNSIDES);
  113.     sunVertices[i][2] = SUNRADIUS * sin(2.0 * M_PI * float(i) / SUNSIDES);
  114.   }
  115.  
  116.   // position stars on a sphere
  117.   if (num < 0) num = 0;
  118.   numStars = num;
  119.   numStarsPerColor = numStars / NUMSTARCOLORS;
  120.   starPositions = (float(*)[3])new float[numStars*3];
  121.   for (i = 0; i < numStars; i++) {
  122.     float x = drand48()-0.5, y = drand48()-0.5, z = drand48()-0.5;
  123.     float d = sqrt(x*x + y*y + z*z);
  124.     if (d < 1e-5 || d > 1.0) i--;
  125.     else {
  126.       starPositions[i][0] = STARRADIUS * x / d;
  127.       starPositions[i][1] = STARRADIUS * y / d;
  128.       starPositions[i][2] = STARRADIUS * z / d;
  129.     }
  130.   }
  131.  
  132.   // make star callback node
  133.   SoCallback* starsCallback = new SoCallback;
  134.   starsCallback->setCallback(starsAction);
  135.  
  136.   return starsCallback;
  137. }
  138.